package com.copa.l264;

/**
 * @author copa
 * @createDate 2022-05-31 16:59
 * @function 动态规划法
 */
public class Test2 {

    public int nthUglyNumber(int n) {
        int[] dp = new int[n+1];
        dp[1] = 1;
        int p2 = 1;
        int p3 = 1;
        int p5 = 1;
        for (int i = 2; i <= n; i++) {
            int t2 = dp[p2] * 2;
            int t3 = dp[p3] * 3;
            int t5 = dp[p5] * 5;
            dp[i] = Math.min(t2, Math.min(t3, t5));

            if (t2 == dp[i]) {
                p2++;
            }
            if (t3 == dp[i]) {
                p3++;
            }
            if (t5 == dp[i]) {
                p5++;
            }
        }
        return dp[n];
    }

    public static void main(String[] args) {
        System.out.println(new Test2().nthUglyNumber(10));
    }
}
